import 'package:file_cache/file_cache.dart';
import 'package:flutter/material.dart';

class FileCacheTestPage extends StatelessWidget {
  const FileCacheTestPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("file cache test"),
      ),
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            MaterialButton(
              onPressed: () async {
                String json = await fileCacheManager.getCache("key1", params: {"p1": 1});
                debugPrint(json);
              },
              child: const Text("get cache by key1, {p1,p1}"),
            ),
            MaterialButton(
              onPressed: () async {
                fileCacheManager
                    .putCache("key1", "json1", params: {"p1": 1})
                    .then((value) => null)
                    .catchError((e) {
                      debugPrint("---------" + e.toString());
                    });
                fileCacheManager
                    .putCache("key1", "json1", params: {"p1": 1})
                    .then((value) => null)
                    .onError((error, stackTrace) {
                      debugPrint(error.toString());
                    })
                    .catchError((e) {
                      debugPrint("---------" + e.toString());
                    });
                fileCacheManager
                    .putCache("key1", "json1", params: {"p1": 1})
                    .then((value) => null)
                    .catchError((e) {
                      debugPrint("---------" + e.toString());
                    });

                // try catch不会捕获异步任务的异常
                // try {
                //   fileCacheManager.putCache("key1", "json1", params: {"p1": 1});
                // } catch (e) {
                //   debugPrint("---${e.toString()}");
                // }
              },
              child: const Text("put cache by key1, {p1,p1}"),
            ),
            MaterialButton(
              onPressed: () async {
                String json = await fileCacheManager.getCache("key1");
                debugPrint(json);
              },
              child: const Text("get cache by key1"),
            ),
            MaterialButton(
              onPressed: () async {
                fileCacheManager.putCache("key1", "json2");
              },
              child: const Text("put cache by key1"),
            ),
          ],
        ),
      ),
    );
  }
}
